package com.lovo.sheyingback.controller;

import com.lovo.sheyingback.bean.Content;
import com.lovo.sheyingback.bean.Image;
import com.lovo.sheyingback.bean.SystemDict;
import com.lovo.sheyingback.bean.Type;
import com.lovo.sheyingback.bean.form.ContentForm;
import com.lovo.sheyingback.bean.form.TypeForm;
import com.lovo.sheyingback.result.ResponseResult;
import com.lovo.sheyingback.service.ContentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@ResponseResult
@RequestMapping("/contents")
@Tag(name = "内容管理ContentController")
public class ContentController {
    @Autowired
    private ContentService contentService;
    @GetMapping
    @Operation(summary = "查询内容")
    public Object listContent(ContentForm contentForm){
        Content content = new Content();
        BeanUtils.copyProperties(contentForm,content);
        if(contentForm.getStatusId() != null){
            content.setStatus(new SystemDict(contentForm.getStatusId()));
        }
        if(contentForm.getTypeId() != null){
            content.setType(new Type(contentForm.getTypeId()));
        }
        if(contentForm.getTitle() != null){
            content.setTitle("%"+contentForm.getTitle()+"%");
        }
        if(contentForm.getText() != null){
            content.setText("%"+contentForm.getText()+"%");
        }
        return contentService.listContent(contentForm.getCurrent(),contentForm.getPageSize(),content);
    }
    @GetMapping("/type")
    @Operation(summary = "根据类别查询内容")
    public Object listContentByType(ContentForm contentForm){
        return contentService.listContentByType(contentForm.getCurrent(),contentForm.getPageSize(),contentForm.getTypeId());
    }
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询")
    public Object getById(@PathVariable Integer id){
        return contentService.getById(id);

    }
    @PostMapping
    @Operation(summary = "增加内容")
    public Object addContent(@RequestBody @Valid ContentForm contentForm){
        Content content = new Content();
        BeanUtils.copyProperties(contentForm,content);
        content.setType(new Type(contentForm.getTypeId()));
        content.setImages(Arrays.stream(contentForm.getImages()).map(item -> new Image(item)).toList());
        return contentService.insert(content);
    }
    @PutMapping("/{id}")
    @Operation(summary = "修改内容")
    public Object updateContent(@PathVariable Integer id,@RequestBody ContentForm contentForm){
        Content content = new Content();
        BeanUtils.copyProperties(contentForm,content);
        content.setId(id);
        if(contentForm.getTypeId() != null){
            content.setType(new Type(contentForm.getTypeId()));
        }
        if (contentForm.getStatusId() != null) {
            content.setStatus(new SystemDict(contentForm.getStatusId()));
        }
        if(contentForm.getImages() != null){
            content.setImages(Arrays.stream(contentForm.getImages()).map(item -> new Image(item)).toList());
        }


        return contentService.update(content);
    }

    @PutMapping("/index/{id}")
    @Operation(summary = "修改内容上移或下移")
    public Object updateContentIndex(@PathVariable Integer id,@RequestBody ContentForm contentForm){
        return contentService.updateIndex(id,contentForm.getVal());
    }
}
